//
//  AppDelegate.m
//  GPSRecord
//
//  Created by TianWan on 2017/1/6.
//  Copyright © 2017年 天玩. All rights reserved.
//

#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#import "Location.h"
#import "BGTask.h"
#import "MainViewController.h"
#import "HomeViewController.h"
#import "SettingViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "WXApi.h"

@interface AppDelegate () <LocationDelegate, UIAlertViewDelegate>
{
    Location *_location;
    NSInteger _alertViewIndex;
}
@property (assign , nonatomic) NSInteger liveTime;
@property (strong , nonatomic) BGTask *task;
@property (strong , nonatomic) NSTimer *bgTimer;
@property (strong , nonatomic) MainViewController *controller;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self weixinLogin];
    [self checkLocationPermission];
    [self checkWx];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    NSString *openID = [[NSUserDefaults standardUserDefaults] objectForKey:kopenID];
    if (!openID) {
        self.window.rootViewController = [[HomeViewController alloc] init];
    }else {
        self.window.rootViewController = [[SettingViewController alloc] init];
    }
    [self.window makeKeyAndVisible];
    
    return YES;

}

#pragma mark 微信登录设置
- (void)weixinLogin
{
    [ShareSDK registerApp:@"1a7851f6a9670"];
    
    [ShareSDK connectWeChatWithAppId:@"wx0fddfd962223cdf8"
                           appSecret:@"43fc59cb3f240ae94527678cdc26e89e"
                           wechatCls:[WXApi class]];
}

#pragma mark  检测是否装了微信
- (void)checkWx
{
    if (![WXApi isWXAppInstalled]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有安装微信客户端，请安装微信后点击登录" preferredStyle:UIAlertControllerStyleAlert];
        [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        [alertController addAction:okAction];
    }
}

#pragma mark 检查定位权限
- (void)checkLocationPermission
{
     if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"应用定位没有打开，需要在在设置/通用/后台应用刷新开启" preferredStyle:UIAlertControllerStyleAlert];
        [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        [alertController addAction:okAction];
    }
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [ShareSDK handleOpenURL:url wxDelegate:nil];
}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [ShareSDK handleOpenURL:url
                 sourceApplication:sourceApplication
                        annotation:annotation
                        wxDelegate:nil];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
